package com.springbootnetty.netty_server;

import com.springbootnetty.netty_handler.MyNettyServerHandler;
import com.springbootnetty.netty_handler.MyNettyServerHeartBeat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/*测试成功接收发送，推送 */

public class MyNettyServer {



    // 管理连接的client
    public static ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    // 管理连接的ChannelHandlerContext
    public static Map<String, ChannelHandlerContext> map = new ConcurrentHashMap<>();


    /**
     * main方法测试
     */
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                MyNettyServer server = new MyNettyServer();
                server.serverStart();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }).start();
        while (true) {
            Thread.sleep(10000);
            ByteBuf byteBuf = Unpooled.copiedBuffer("xyz ", CharsetUtil.UTF_8);
            clients.writeAndFlush(byteBuf);
            System.out.println(clients);
            MyNettyServer.map.get("192.168.0.163").channel().writeAndFlush(byteBuf);
        }

    }


    /***
     * 启动服务端
     * */
    public void serverStart() {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1); // 负责连接 用来accept
        EventLoopGroup workerGroup = new NioEventLoopGroup(2); // 负责处理
        ServerBootstrap sb = new ServerBootstrap(); // 辅助启动类
        try {
            ChannelFuture f = sb.group(bossGroup, workerGroup) // 指定线程池组
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel){
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline .addLast(new MyNettyServerHandler());
                            //读超时时间设置为100s，0表示不监控
                            pipeline .addLast(new IdleStateHandler(600, 0, 0, TimeUnit.SECONDS));
                            //加入处理事件
                            pipeline .addLast(new MyNettyServerHeartBeat());
                        }
                    })
                    .bind(7611)
                    .sync();
            f.channel().closeFuture().sync(); // close->ChannelFuture
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }



}














